﻿package smooth.core.canvas.multimediaCanvas {
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import caurina.transitions.Tweener;	
	import smooth.core.object.flickrComponent.FlickrComponent;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	public class ChangeDisplayComponent extends Sprite {
		
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		private var clickgrabber:Shape = new Shape();	
		public var initialPosX:int;
		public var initialPosY:int;
		private var canvas:Object;
		private var arrangeIndex:int = 0;
		
		public function ChangeDisplayComponent(canvas:Object):void {
			this.canvas = canvas;
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}
		
		
		private function initGraphics():void {
			var imageLoader:Loader =new Loader();
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,resfreshIconLoadComplete);
			imageLoader.load(new URLRequest("../resources/refresh.png"));
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_DOWN, this.touchDown);
		}
		
		private function resfreshIconLoadComplete(event:Event):void {
			// Draw circle for close button
			clickgrabber.graphics.lineStyle(4,0xFFFFFF, 1);
			clickgrabber.graphics.beginFill(0xFFFFFF, 0.5);
			clickgrabber.graphics.drawCircle(0, 0, 45);
			clickgrabber.graphics.endFill();
			this.addChild(clickgrabber);
			// Add icon
			var resfreshIcon:Bitmap = new Bitmap();
			resfreshIcon.bitmapData = event.target.content.bitmapData;
			resfreshIcon.alpha = 0.5;
			resfreshIcon.width = 80;
			resfreshIcon.height = 80;
			resfreshIcon.x = -40;
			resfreshIcon.y = -40;
			resfreshIcon.alpha = 1;
			this.addChild(resfreshIcon);	
		}
		
		private function touchDown(e:TouchEvent):void {
			this.doChangeDisplay();
		}
		
		public function doChangeDisplay():void {
			switch (this.arrangeIndex) {
				case 0 :
					this.sprialPhotos();
					break;
				case 1 :
					this.fanPhotos2();
					break;
				case 2 :
					this.fanPhotos6();
					break;
			}
			if (this.arrangeIndex < 2)
				this.arrangeIndex++;
			else	
				this.arrangeIndex = 0;
		}
		
		private function sprialPhotos():void {
			var n:int = this.canvas.multimediaObjects.length;
			for (var i:int = 0; i < n; i++) {
				var child:DisplayObject = this.canvas.multimediaObjects[i];
				minX = 0, maxX = this.canvas.width;
				minY = 0, maxY = this.canvas.height;
				var targetX:int = this.canvas.width/2;
				var targetY:int = this.canvas.height / 2;
				var targetRotation:int = 0;	
				var targetScale:Number = 0.5;	    
				Tweener.addTween(child, {scaleX: targetScale, scaleY: targetScale, rotation:targetRotation, x:targetX, y:targetY, time:0.35, transition:"easeinoutquad"});	
			}
		}	
		
		private function fanPhotos2():void {	
			var n:int = this.canvas.multimediaObjects.length;
			var substract:int = 33 * n/2-this.canvas.width/2;
			for(var i:int = 0; i<n; i++) {
				var child:DisplayObject = this.canvas.multimediaObjects[i];
				minX = 0, maxX = this.canvas.width;
				minY = 0, maxY = this.canvas.height;
				var targetX:int = 33*i-substract;
				var targetY:int = this.canvas.height / 2 + 5 * i;
				var targetRotation:int = 0;	
				var targetScale:Number = 0.5;	
				Tweener.addTween(child, {scaleX: targetScale, scaleY: targetScale, rotation:targetRotation, x:targetX, y:targetY, time:0.65, transition:"easeinoutquad"});	
			}
		}	
		
		private function fanPhotos6():void {	
			var n:int = this.canvas.multimediaObjects.length;
			var r:Number = this.canvas.width/4; 
			var p:Number = 2*Math.PI/8;
			for(var i:int = 0; i<n; i++) {
				var child:DisplayObject = this.canvas.multimediaObjects[i];
				var x1:int= this.canvas.width/3+r*Math.cos(i*p);
				var y1:int = this.canvas.height/2+r*Math.sin(i*p);
				var d:Number = 2*Math.PI*r/8;
				var targetX:int = x1;
				var targetY:int = y1;
				var targetRotation:int = 0;	
				var targetScale:Number = 0.5;	    
				Tweener.addTween(child, {scaleX: targetScale, scaleY: targetScale, rotation:targetRotation, x:targetX, y:targetY, time:0.65, transition:"easeinoutquad"});	
			}
		}
		
		
	}
	
}